package com.qiyi.components.preview.container
{
   import flash.display.Sprite;
   import com.qiyi.components.preview.media.PrevewVideo;
   import flash.display.Shape;
   import flash.text.TextField;
   import com.qiyi.components.preview.events.MediaPlayerEvent;
   import flash.text.AntiAliasType;
   import flash.text.TextFormat;
   import flash.text.TextFormatAlign;
   import gs.TweenLite;
   import flash.geom.Rectangle;
   import com.qiyi.components.preview.events.PreviewEvent;
   import com.qiyi.components.preview.media.MediaState;
   
   public class PreviewSprite extends Sprite
   {
      
      public function PreviewSprite() {
         super();
         this.initUI();
      }
      
      public var player:PrevewVideo;
      
      private var back:Shape;
      
      private var timeTxt:TextField;
      
      private var timeBackGround:Shape;
      
      private var loading:PreviewLoading;
      
      private var _time:Number = 0;
      
      private var _currentX:Number = 0;
      
      private var _gap:Number = 0;
      
      private var _realWid:Number = 0;
      
      private function initUI() : void {
         this.back = new Shape();
         this.back.graphics.clear();
         this.back.graphics.lineStyle(0.5,3355443);
         this.back.graphics.beginFill(0,2);
         this.back.graphics.drawRect(0,0,Preview.RECT_0.width,Preview.RECT_0.height);
         this.back.graphics.endFill();
         addChild(this.back);
         this.back.x = -Preview.RECT_0.width / 2;
         this.back.y = -Preview.RECT_0.height;
         this.player = new PrevewVideo();
         addChild(this.player);
         this.player.addEventListener(MediaPlayerEvent.MEDIA_STATE_CHANGE,this.onMediaBufferFull);
         this.player.setVideoSize(Preview.RECT_0.width,Preview.RECT_0.height);
         this.player.x = -Preview.RECT_0.width / 2;
         this.player.y = -Preview.RECT_0.height;
         this.player.visible = false;
         this.timeBackGround = new Shape();
         this.timeBackGround.graphics.clear();
         this.timeBackGround.graphics.beginFill(0,1);
         this.timeBackGround.graphics.drawRect(0,0,Preview.RECT_0.width,Preview.RECT_0.height);
         this.timeBackGround.graphics.endFill();
         addChild(this.timeBackGround);
         this.timeBackGround.x = -Preview.RECT_0.width / 2;
         this.timeBackGround.y = -Preview.RECT_0.height;
         this.timeTxt = new TextField();
         this.timeTxt.antiAliasType = AntiAliasType.ADVANCED;
         this.timeTxt.mouseEnabled = false;
         var _loc1_:TextFormat = new TextFormat();
         _loc1_.align = TextFormatAlign.CENTER;
         _loc1_.font = "Arial";
         _loc1_.size = 11;
         _loc1_.color = 16777215;
         this.timeTxt.defaultTextFormat = _loc1_;
         addChild(this.timeTxt);
         this.timeTxt.width = Preview.RECT_0.width;
         this.timeTxt.height = Preview.RECT_0.height;
         this.timeTxt.x = -Preview.RECT_0.width / 2;
         this.timeTxt.y = -Preview.RECT_0.height;
         this.loading = new PreviewLoading();
         this.loading.mouseEnabled = false;
         addChild(this.loading);
         this.loading.visible = false;
      }
      
      public function set currentX(param1:Number) : void {
         this._currentX = param1;
      }
      
      public function hidePrivew() : void {
         this.loading.visible = false;
         this.resetAll();
         this.smallSize();
      }
      
      public function previewSpot(param1:String, param2:Number, param3:Number = 0) : void {
         this.loading.visible = false;
         this._currentX = param3;
         this.resetAll();
         this.spotSmallSize();
         this.timeTxt.text = param1;
         this._time = int(param2);
      }
      
      public function previewTime(param1:String, param2:Number, param3:Number = 0) : void {
         this.loading.visible = false;
         this._currentX = param3;
         this.resetAll();
         this.smallSize();
         this.timeTxt.text = param1;
         this._time = int(param2);
      }
      
      public function previewSpotVideo() : void {
         this.loading.visible = true;
         this.loading.scaleX = 0;
         this.loading.scaleY = 0;
         this.loading.alpha = 0;
         this.resetAll();
         this.player.play(this._time);
         this.spotMiddleSize();
      }
      
      public function previewVideo() : void {
         this.loading.visible = true;
         this.loading.scaleX = 0;
         this.loading.scaleY = 0;
         this.loading.alpha = 0;
         this.resetAll();
         this.player.play(this._time);
         this.middleSize();
      }
      
      private function spotSmallSize() : void {
         var WID:Number = NaN;
         TweenLite.killTweensOf(this.back);
         TweenLite.killTweensOf(this.player);
         TweenLite.killTweensOf(this.timeTxt);
         TweenLite.killTweensOf(this.timeBackGround);
         this.timeBackGround.visible = false;
         this.timeTxt.visible = true;
         this.player.visible = false;
         this.back.visible = true;
         this.back.alpha = 0.8;
         var value:Number = 0;
         try
         {
            WID = Preview.SPOT_WID + 4;
            if(this._currentX < WID / 2 - this.gap)
            {
               value = WID / 2 + this.gap - this._currentX;
            }
            else if(this._currentX > this._realWid - this.gap - WID / 2)
            {
               value = -(WID / 2 - (this._realWid - this.gap - this._currentX));
            }
            
         }
         catch(e:Error)
         {
         }
         (this.parent as Preview).setSpotOffset(value);
         this.back.width = WID;
         this.back.height = Preview.RECT_0.height + Preview.SPOT_HEI + 5;
         this.back.x = -this.back.width / 2 + value;
         this.back.y = -Preview.RECT_0.height - Preview.SPOT_HEI - 5;
         TweenLite.to(this.timeTxt,0.2,
            {
               "x":-Preview.RECT_0.width / 2 + value,
               "y":-Preview.RECT_0.height
            });
         this.player.x = -Preview.RECT_0.width / 2 + value;
         this.player.y = -Preview.RECT_0.height;
         this.player.setVideoSize(Preview.RECT_0.width,Preview.RECT_0.height);
         this.timeBackGround.x = this.back.x;
         this.timeBackGround.y = -Preview.RECT_0.height;
         this.timeBackGround.width = this.back.width;
         this.timeBackGround.height = Preview.RECT_0.height;
         this.timeBackGround.alpha = 1;
      }
      
      private function smallSize() : void {
         this.timeBackGround.visible = false;
         this.timeTxt.visible = true;
         this.player.visible = false;
         this.back.visible = true;
         this.back.alpha = 0.8;
         var value:Number = 0;
         try
         {
            if(this._currentX < Preview.RECT_0.width / 2)
            {
               value = Preview.RECT_0.width / 2 + this.gap - this._currentX;
            }
            else if(this._currentX > this._realWid - this.gap - Preview.RECT_0.width / 2)
            {
               value = -(Preview.RECT_0.width / 2 - (this._realWid - this.gap - this._currentX));
            }
            
         }
         catch(e:Error)
         {
         }
         TweenLite.to(this.back,0.2,
            {
               "x":-Preview.RECT_0.width / 2 + value,
               "y":-Preview.RECT_0.height,
               "width":Preview.RECT_0.width,
               "height":Preview.RECT_0.height
            });
         TweenLite.to(this.timeTxt,0.2,
            {
               "x":-Preview.RECT_0.width / 2 + value,
               "y":-Preview.RECT_0.height
            });
         this.player.x = -Preview.RECT_0.width / 2 + value;
         this.player.y = -Preview.RECT_0.height;
         this.player.setVideoSize(Preview.RECT_0.width,Preview.RECT_0.height);
         this.timeBackGround.x = -Preview.RECT_0.width / 2 + value;
         this.timeBackGround.y = -Preview.RECT_0.height;
         this.timeBackGround.width = Preview.RECT_0.width;
         this.timeBackGround.height = Preview.RECT_0.height;
         this.timeBackGround.alpha = 1;
      }
      
      public function spotMiddleSize() : void {
         this.back.alpha = 0.8;
         this.player.visible = true;
         this.timeTxt.visible = true;
         this.timeBackGround.visible = true;
         var value:Number = 0;
         try
         {
            if(this._currentX < Preview.RECT_1.width / 2 - this.gap)
            {
               value = Preview.RECT_1.width / 2 + this.gap - this._currentX;
            }
            else if(this._currentX > this._realWid - this.gap - Preview.RECT_1.width / 2)
            {
               value = -(Preview.RECT_1.width / 2 - (this._realWid - this.gap - this._currentX));
            }
            
         }
         catch(e:Error)
         {
         }
         (this.parent as Preview).setSpotOffset(value);
         TweenLite.to(this.back,0.3,
            {
               "x":-Preview.RECT_1.width / 2 + value,
               "y":-Preview.RECT_1.height - Preview.SPOT_HEI,
               "width":Preview.RECT_1.width,
               "height":Preview.RECT_1.height + Preview.SPOT_HEI,
               "onComplete":this.onMiddleComplete,
               "onCompleteParams":[value]
            });
         TweenLite.to(this.timeTxt,0.3,
            {
               "x":-Preview.RECT_1.width / 2 + 2 + value,
               "y":-Preview.RECT_0.height - Preview.SPOT_HEI
            });
         TweenLite.to(this.timeBackGround,0.3,
            {
               "x":-Preview.RECT_1.width / 2 + 1 + value,
               "y":-Preview.RECT_0.height - Preview.SPOT_HEI,
               "width":Preview.RECT_1.width - 2,
               "height":Preview.RECT_0.height
            });
         var rect:Rectangle = this.checkplayerRect(new Rectangle(-Preview.RECT_1.width / 2 + 2 + value,-Preview.RECT_1.height - Preview.SPOT_HEI + 2,Preview.RECT_1.width - 4,Preview.RECT_1.height - 4));
         TweenLite.to(this.player,0.3,
            {
               "x":rect.x,
               "y":rect.y,
               "width":rect.width,
               "height":rect.height,
               "alpha":1
            });
         TweenLite.to(this.loading,0.3,
            {
               "scaleX":1,
               "scaleY":1,
               "alpha":1,
               "x":value,
               "y":-(Preview.RECT_1.height + Preview.SPOT_HEI) / 2
            });
      }
      
      public function middleSize() : void {
         this.back.alpha = 0.8;
         this.player.visible = true;
         this.timeTxt.visible = true;
         this.timeBackGround.visible = true;
         var value:Number = 0;
         try
         {
            if(this._currentX < Preview.RECT_1.width / 2 - this.gap)
            {
               value = Preview.RECT_1.width / 2 + this.gap - this._currentX;
            }
            else if(this._currentX > this._realWid - this.gap - Preview.RECT_1.width / 2)
            {
               value = -(Preview.RECT_1.width / 2 - (this._realWid - this.gap - this._currentX));
            }
            
         }
         catch(e:Error)
         {
         }
         (this.parent as Preview).setSpotOffset(value);
         var rect:Rectangle = this.checkplayerRect(new Rectangle(-Preview.RECT_1.width / 2 + 2 + value,-Preview.RECT_1.height + 2,Preview.RECT_1.width - 4,Preview.RECT_1.height - 4));
         if(this.back.width != Preview.RECT_1.width)
         {
            TweenLite.to(this.back,0.3,
               {
                  "x":-Preview.RECT_1.width / 2 + value,
                  "y":-Preview.RECT_1.height,
                  "width":Preview.RECT_1.width,
                  "height":Preview.RECT_1.height,
                  "onComplete":this.onMiddleComplete,
                  "onCompleteParams":[value]
               });
            TweenLite.to(this.timeTxt,0.3,
               {
                  "x":-Preview.RECT_1.width / 2 + 2 + value,
                  "y":-Preview.RECT_0.height
               });
            TweenLite.to(this.timeBackGround,0.3,
               {
                  "x":-Preview.RECT_1.width / 2 + 1 + value,
                  "y":-Preview.RECT_0.height,
                  "width":Preview.RECT_1.width - 2,
                  "height":Preview.RECT_0.height
               });
            TweenLite.to(this.player,0.3,
               {
                  "x":rect.x,
                  "y":rect.y,
                  "width":rect.width,
                  "height":rect.height,
                  "alpha":1
               });
            TweenLite.to(this.loading,0.3,
               {
                  "scaleX":1,
                  "scaleY":1,
                  "alpha":1,
                  "x":value,
                  "y":-Preview.RECT_1.height / 2
               });
         }
         else
         {
            this.player.x = rect.x;
            this.player.y = rect.y;
            this.player.setVideoSize(rect.width,rect.height);
         }
      }
      
      public var _enableMoveIn:Boolean;
      
      public function get enableMoveIn() : Boolean {
         return this._enableMoveIn;
      }
      
      private function onMiddleComplete(param1:Number) : void {
         this._enableMoveIn = true;
         dispatchEvent(new PreviewEvent(PreviewEvent.PREVIEW_VIDEO_WND_SHOW));
      }
      
      private function onBigComplete(param1:Number) : void {
      }
      
      public function bigSize() : void {
         TweenLite.killTweensOf(this.back);
         TweenLite.killTweensOf(this.player);
         TweenLite.killTweensOf(this.timeTxt);
         TweenLite.killTweensOf(this.timeBackGround);
         this.timeTxt.visible = true;
         this.player.visible = true;
         var value:Number = 0;
         try
         {
            if(this._currentX < Preview.RECT_2.width / 2 - this.gap)
            {
               value = Preview.RECT_2.width / 2 + this.gap - this._currentX;
            }
            else if(this._currentX > this._realWid - Preview.RECT_2.width / 2)
            {
               value = -(Preview.RECT_2.width / 2 - (this._realWid - this.gap - this._currentX));
            }
            
         }
         catch(e:Error)
         {
         }
         (this.parent as Preview).setSpotOffset(value);
         TweenLite.to(this.back,0.3,
            {
               "x":-Preview.RECT_2.width / 2 + value,
               "y":-Preview.RECT_2.height,
               "width":Preview.RECT_2.width,
               "height":Preview.RECT_2.height,
               "onComplete":this.onBigComplete,
               "onCompleteParams":[value]
            });
         TweenLite.to(this.timeTxt,0.3,
            {
               "x":-Preview.RECT_2.width / 2 + 2 + value,
               "y":-Preview.RECT_0.height
            });
         TweenLite.to(this.timeBackGround,0.3,
            {
               "x":-Preview.RECT_2.width / 2 + 1 + value,
               "y":-Preview.RECT_0.height,
               "width":Preview.RECT_2.width - 2,
               "height":Preview.RECT_0.height
            });
         var rect:Rectangle = this.checkplayerRect(new Rectangle(-Preview.RECT_2.width / 2 + 2 + value,-Preview.RECT_2.height + 2,Preview.RECT_2.width - 4,Preview.RECT_2.height - 4));
         TweenLite.to(this.player,0.3,
            {
               "x":rect.x,
               "y":rect.y,
               "width":rect.width,
               "height":rect.height,
               "alpha":1
            });
         TweenLite.to(this.loading,0.3,
            {
               "scaleX":1,
               "scaleY":1,
               "alpha":1,
               "x":value,
               "y":-Preview.RECT_2.height / 2
            });
      }
      
      public function spotBigSize() : void {
         TweenLite.killTweensOf(this.back);
         TweenLite.killTweensOf(this.player);
         TweenLite.killTweensOf(this.timeTxt);
         TweenLite.killTweensOf(this.timeBackGround);
         this.timeTxt.visible = true;
         this.player.visible = true;
         var value:Number = 0;
         try
         {
            if(this._currentX < Preview.RECT_2.width / 2 - this.gap)
            {
               value = Preview.RECT_2.width / 2 + this.gap - this._currentX;
            }
            else if(this._currentX > this._realWid - Preview.RECT_2.width / 2)
            {
               value = -(Preview.RECT_2.width / 2 - (this._realWid - this._currentX));
            }
            
         }
         catch(e:Error)
         {
         }
         (this.parent as Preview).setSpotOffset(value);
         TweenLite.to(this.back,0.3,
            {
               "x":-Preview.RECT_2.width / 2 + value,
               "y":-Preview.RECT_2.height - Preview.SPOT_HEI,
               "width":Preview.RECT_2.width,
               "height":Preview.RECT_2.height + Preview.SPOT_HEI,
               "onComplete":this.onBigComplete,
               "onCompleteParams":[value]
            });
         TweenLite.to(this.timeTxt,0.3,
            {
               "x":-Preview.RECT_2.width / 2 + 2 + value,
               "y":-Preview.RECT_0.height - Preview.SPOT_HEI
            });
         TweenLite.to(this.timeBackGround,0.3,
            {
               "x":-Preview.RECT_2.width / 2 + 1 + value,
               "y":-Preview.RECT_0.height - Preview.SPOT_HEI,
               "width":Preview.RECT_2.width - 2,
               "height":Preview.RECT_0.height
            });
         var rect:Rectangle = this.checkplayerRect(new Rectangle(-Preview.RECT_2.width / 2 + 2 + value,-Preview.RECT_2.height - Preview.SPOT_HEI + 2,Preview.RECT_2.width - 4,Preview.RECT_2.height - 4));
         TweenLite.to(this.player,0.3,
            {
               "x":rect.x,
               "y":rect.y,
               "width":rect.width,
               "height":rect.height,
               "alpha":1
            });
         TweenLite.to(this.loading,0.3,
            {
               "scaleX":1,
               "scaleY":1,
               "alpha":1,
               "x":value,
               "y":-(Preview.RECT_2.height + Preview.SPOT_HEI) / 2
            });
      }
      
      public function previewAllStage(param1:Number, param2:Number) : void {
         var param2:Number = param2 - this.mouseY;
         this.resetAll();
         this.player.visible = true;
         this.loading.visible = false;
         this.back.alpha = 0;
         this.timeTxt.visible = false;
         this.timeBackGround.visible = false;
         var _loc3_:Rectangle = this.checkplayerRect(new Rectangle(-param1,-param2,this._realWid,param2));
         TweenLite.to(this.player,1.3,
            {
               "x":_loc3_.x,
               "y":_loc3_.y,
               "width":_loc3_.width,
               "height":_loc3_.height,
               "alpha":0.4,
               "onComplete":this.onPreviewAllStageComplete
            });
      }
      
      private function onPreviewAllStageComplete() : void {
         this.player.visible = false;
         dispatchEvent(new PreviewEvent(PreviewEvent.PREVIEW_ALL_STAGE));
      }
      
      private function checkplayerRect(param1:Rectangle) : Rectangle {
         var rect:Rectangle = null;
         var r:Rectangle = param1;
         try
         {
            rect = new Rectangle();
            if(r.width / r.height > this.player.aspect)
            {
               rect.height = r.height;
               rect.width = r.height * this.player.aspect;
            }
            else
            {
               rect.width = r.width;
               rect.height = r.width / this.player.aspect;
            }
            rect.x = r.x + (r.width / 2 - rect.width / 2);
            rect.y = r.y + (r.height / 2 - rect.height / 2);
            return rect;
         }
         catch(e:Error)
         {
            return new Rectangle(0,0,0,0);
         }
         return new Rectangle(0,0,0,0);
      }
      
      public function resetAll(param1:Boolean = true) : void {
         if(param1)
         {
            TweenLite.killTweensOf(this.back);
            TweenLite.killTweensOf(this.player);
            TweenLite.killTweensOf(this.timeTxt);
            TweenLite.killTweensOf(this.timeBackGround);
            this._enableMoveIn = false;
         }
         this.player.close();
      }
      
      private function onMediaBufferFull(param1:MediaPlayerEvent) : void {
         if(param1.state == MediaState.MEDIA_FULL)
         {
            this.timeBackGround.alpha = 0.6;
            this.loading.visible = false;
         }
      }
      
      public function set gap(param1:Number) : void {
         this._gap = param1;
      }
      
      public function get gap() : Number {
         return this._gap;
      }
      
      public function set realWidth(param1:Number) : void {
         this._realWid = param1;
      }
      
      public function get realWidth() : Number {
         return this._realWid;
      }
   }
}
